Strict

Import agfx

Global myTmpScreen:TTmpScreen

Class TAgfx_test_app Extends TFramework

	Method OnCreate:Int()
		
		'DEVICE.SetFps(30)		' default FPS is 60
		
		Super.OnCreate()
				
		myTmpScreen = New TTmpScreen		
		myTmpScreen.Prepare()
		
		Return 0
	End
	
	
End


Class TTmpScreen Extends TScreen

    Field width:Int = 640
    Field height:Int = 480
    Field name:String = "Base"

	
	Field world:b2World
	
	' static ground'
	Field groundBox:b2PolygonShape
	Field groundBodyDef:b2BodyDef
	Field groundBody:b2Body
    Field m_physScale:Float = 30

    ' dynamic body
    Field body:b2Body
    Field dynamicBox:b2PolygonShape
    Field fixtureDef:b2FixtureDef

	' START when app is created
	Method Start:Void()

		' [1] set gravity vector
		
		 Local gravity :b2Vec2 = New b2Vec2(0.0, 10.0)
		 Local doSleep:Bool = True

		' [2] Construct a world object
        
        Self.world = New b2World(gravity, doSleep)
        Self.world.SetWarmStarting(True)

        ' [3] Create GROUD

        Self.groundBox = New b2PolygonShape()
        Self.groundBodyDef = New b2BodyDef()
        Self.groundBodyDef.position.Set(0, 30.0)        
        Self.groundBox.SetAsBox(50.0, 10.0)
        Self.groundBody = world.CreateBody(Self.groundBodyDef)
        Self.groundBody.CreateFixture2(Self.groundBox)

        ' [4] Create Dynamic Body
        
        Local bodyDef:b2BodyDef = New b2BodyDef()
        bodyDef.type = b2Body.b2_Body
        bodyDef.position.Set(10, 4.0)
		bodyDef.angle = DEG2RAD*50.0

        Self.body = world.CreateBody(bodyDef)
        Self.dynamicBox = New b2PolygonShape()
        Self.dynamicBox.SetAsBox(1.0,1.0)

        Self.fixtureDef = New b2FixtureDef() 
        Self.fixtureDef.shape = Self.dynamicBox
        Self.fixtureDef.density = 1.0
		Self.fixtureDef.friction = 0.3
		
		Self.body.CreateFixture(Self.fixtureDef)


		Local dbgDraw :b2DebugDraw = New b2DebugDraw()
        
        'dbgDraw.SetSprite(m_sprite)
        dbgDraw.SetDrawScale(10.0)
        dbgDraw.SetFillAlpha(0.3)
        dbgDraw.SetLineThickness(1.0)
        dbgDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit | b2DebugDraw.e_pairBit)
        Self.world.SetDebugDraw(dbgDraw)


		
	End
	
	' main app logic loop update
	Method Update:Void()

		Self.world.TimeStep(1/30.0, 6, 2)

		Self.world.ClearForces()

		Local position:b2Vec2 = body.GetPosition()

		Local angle:Float = Self.body.GetAngle()

		Print("["+position.x+","+position.y+"] ang: "+angle)
	End
	
	' main app render loop
	Method Render:Void()
		Self.world.DrawDebugData()
  	End
End
